package com.dayuanit.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayuanit.dao.CartDao;
import com.dayuanit.domain.ShoppingCart;
import com.dayuanit.domain.User;
import com.dayuanit.exception.MallException;
import com.dayuanit.service.CartService;
import com.dayuanit.vo.CartVO;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	private CartDao cartDao;
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean addCart(CartVO cartVO, HttpServletRequest req) {
		ShoppingCart cart = new ShoppingCart();
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("logined");
		cart.setUserId(user.getId());
		cart.setProductId(cartVO.getProductId());
		cart.setProductType(cartVO.getProductType());
		cart.setProductCount(cartVO.getProductCount());
		cart.setState(1);
		int row = cartDao.addCart(cart);
		if(1 != row) {
			throw new MallException("购物车中增加商品失败");
		}
		return true;
	}

	@Override
	public boolean modfiyCount(ShoppingCart cart) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ShoppingCart getCart() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ShoppingCart> listCarts(HttpServletRequest req) {
		// TODO Auto-generated method stub
		return null;
	}

}
